#include "countingtab.h"
#include "ui_countingtab.h"

CountingTab::CountingTab(QWidget *parent) :
    QWidget(parent), m_ui(new Ui::CountingTab),
    imgL(0), sCounter(new SeedCounter),counted(false)

{
    m_ui->setupUi(this);
    connect(m_ui->countButton, SIGNAL(clicked()), this, SLOT(count()));
    connect(m_ui->totalButton, SIGNAL(clicked()), this, SLOT(total()));
}

CountingTab::~CountingTab()
{
    delete m_ui;
    if(sCounter) delete sCounter;
}

void CountingTab::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CountingTab::addLoader(ImageLoader *loader)
{
    if(loader)
    {
        m_ui->gridLayout->addWidget(loader, 0, 0);
        imgL = loader;
    }
}

void CountingTab::showContent()
{
    if(imgL  != 0)
        m_ui->gridLayout->addWidget(imgL, 0, 0);
}

/*
 *	When count button has been clicked, start counting number of 
 *	seeds in current image.
 *
 */
void CountingTab::count()
{
    int total = 0;
    if(imgL)
    {
		// Clear any displayed image on the right side of image loader
        imgL->clearDisplay();
		// Current displaying image on the left side of image loader
        QString currFile = imgL->getCurrFile();
        if(currFile.length() > 0)
        {
			// Make sure current file is of particular type in order to count
            if(sCounter->addFile(currFile))
            {
                counted = true;
                total = sCounter->numberOfSeeds();
                m_ui->sTotalLabel->setText(QString::number(total));
				// Add generated result image to display
                imgL->addResultImg(sCounter->getResultImg());
                // Assume each dish contains exactly 50 seeds. If counted 
				// total is greater than 50, then subtract from 50; if counted
				// total is less than 50, then subtracted it from 50.
                if(total > 50)
                    m_ui->missLabel->setText(QString::number(total - 50));
                else
                    m_ui->missLabel->setText(QString::number(50 - total));
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setText("This type of experiment is not supported!  ");
                msgBox.exec();
            }
        }
    }    
}

/*
 *	This method try to count all number of seeds in each image accross entire
 *	project. Again for each image, assuming a total of 50 seeds. 
 *
 */
void CountingTab::total()
{
    int total = 0;
    int seeds = 0;

    // Get all images in image loader that are currently displayed 
    QVector<QString> files = imgL->getFiles();
	// Check if there is any file at all
    if(files.size() > 0)
    {
        QString file;
		// Loop through each file in all files
        for(int i = 0; i < files.size(); i++)
        {
			// Get one file at a time
            file = files.at(i);
			// Then add it seed counter for counting
            sCounter->addFile(file);
			// Get counting result back
            seeds = sCounter->numberOfSeeds();
			// Add number of counted seeds for current image
            total += seeds;
        }
        if(total >= 0)            
            m_ui->sTotalLabel_2->setText(QString::number(total));
    }
    else
    {
        qDebug() << "Error";
    }
}

void CountingTab::hasCount()
{
    if(!counted)
        count();
    emit seedCounted(sCounter);
}

